package com.lagou.edu.controller;

import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("toLogin")
    public String toLogin() {
        System.out.println("========>>>>跳转登录页面");
        return "login";
    }

    @RequestMapping("loginSystem")
    public String loginSystem(String username, String password, HttpSession session) {
        // 合法用户，信息写入session，同时跳转到系统主页面
        if("admin".equals(username) && "admin".equals(password)) {
            System.out.println("合法用户");
            session.setAttribute("username",username + System.currentTimeMillis());
            return "redirect:/resume/result";
        }else{
            // 非法用户返回登录页面
            System.out.println("非法，跳转");
            return "redirect:/resume/toLogin";
        }
    }

    @RequestMapping("result")
    public ModelAndView queryAll() throws Exception {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("resumeList",resumeService.queryResumeList());
        modelAndView.setViewName("resumeList");
        return modelAndView;
    }


//    @RequestMapping(value = "/login",method = {RequestMethod.POST})
//    public String login(HttpServletResponse resq, String username, String password){
//
//        if(username.equals("admin") && password.equals("admin")){
//            System.out.println("login!");
//
//            Cookie cookie = new Cookie("username", username);
//            cookie.setMaxAge(300);//设置5分钟有效
//            resq.addCookie(cookie);
//
//            return "redirect:queryAll";
//        }else{
//            return "login";
//        }
//    }


//    @RequestMapping("/queryAll")
////    @ResponseBody
//    public ModelAndView queryAll() throws Exception {
////        return resumeService.queryResumeList();
//
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("resumeList",resumeService.queryResumeList());
//        modelAndView.setViewName("resumeList");
//        return modelAndView;
//    }
}
